package com.tidc.seaserverservice.util;

import com.tidc.seaservermapper.dao.MenuDao;
import com.tidc.seaservermapper.dao.RoleDao;
import com.tidc.seaservermapper.dao.UserDao;
import com.tidc.seaservermodel.dto.UserDto;
import com.tidc.seaservermodel.entity.Menu;
import com.tidc.seaservermodel.entity.Role;
import com.tidc.seaservermodel.entity.User;
import com.tidc.seaservermodel.exception.TidcBadParent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020/9/12 20:09
 * @description：
 * @modified By：
 * @version:
 */
@Component
@Slf4j
public class UserUtil {
    @Resource
    private UserDao userDao;
    @Resource
    private RoleDao roleDao;
    @Resource
    private MenuDao menuDao;
    public UserDto getUserInfo(User user) {
        UserDto userDto = null;
        List<Role> roles = roleDao.withUserIdGetRoles(user.getId());

        if (roles.size()!=0) {
            List<Menu> menus = menuDao.withRoleListGetMenu(roles);
            if(menus.size()!=0){
                userDto = new UserDto();
                userDto.setUser(user);
                userDto.setRoles(roles);
                userDto.setMenus(menus);
            }else{
                log.warn("id {} 用户权限列表异常没有权限",user.getId());
            }
        }else{
            log.warn("id {} 用户角色列表异常没有角色",user.getId());
        }

        return userDto;
    }
    public User getUser(Authentication authentication){
        return userDao.queryById(Integer.valueOf(authentication.getName()));
    }
    public boolean checkUserRepetition(String username,String phone) throws TidcBadParent {
        int i = userDao.findingDuplicateByUsernameAndPhone(username, phone);
        return i>0;
    }
}
